package com.suray.basic.wcs.plc.pojo.oper;

import com.suray.basic.wcs.plc.core.PlcOffset;
import com.suray.basic.wcs.plc.enums.Location;
import com.suray.basic.wcs.plc.enums.PlcDeviceType;
import com.suray.basic.wcs.plc.pojo.DeviceBase;
import com.suray.basic.wcs.utils.Coord;

/**
 * @author shaozn
 * @date 2020/1/22 - 16:08
 * @Description 扫码设备
 */
public class SweepCodeEquipmentOper extends DeviceBase {

    private static final PlcOffset CAN_READ = new PlcOffset(0, null);
    private final int maxBarCode = 15;
    // 西门子
    private final static int highBit = 8;


    private final static int lowBit = 0;
    /**
     * 可读取
     * 0:无效  1:可读
     */
    private boolean canRead;
    /**
     * 扫码结果数据
     */
    private String codeData;


    public SweepCodeEquipmentOper(PlcDeviceType plcDeviceType, int startNo, String deviceName, int deviceNo, Coord coord,
                                  Location location, Coord lockLocation, long deviceId, Integer gridzMax,
                                  Integer gridzRelative, int plcId) {
        super(plcDeviceType, startNo, deviceName, deviceNo, coord, location, lockLocation, deviceId, gridzMax,
                gridzRelative, plcId);
    }

    public boolean isCanRead() {
        return readPlc(CAN_READ) == 1;
    }

    public void setCanRead(boolean canRead) {
        this.canRead = canRead;
    }

    /**
     * 获取扫码结果
     *
     * @return 条码值
     */
//    public String getCodeData() {
//        StringBuilder sb = new StringBuilder();
//        for (int i = 3; i < maxBarCode; i++) {
//            sb.append((char) getBarCodeByBit(i, highBit));
//            sb.append((char) getBarCodeByBit(i, lowBit));
//        }
//        System.out.println("pre条码值："+sb);
//        int end = sb.lastIndexOf((char) 03 + "");
//        int start = sb.lastIndexOf((char) 02 + "");
//
//        if (((char) 00 + "").equals(sb.charAt(0) + "")) {
//            return null;
//        }
//        if (end == -1 || start == -1 || start > end) {
//            return "error";
//        }
//        sb = sb.deleteCharAt(end);
//        sb = sb.delete(end, sb.length());
//        System.out.println("aft条码值："+sb.toString().substring(1));
//        return sb.toString().substring(1);
//    }

    /**
     * 获取扫码结果
     * 起始字节为扫码值有效值，后面15个字节用于码值解析
     * 码值解析按byte读取，0x2为起始标志，0x3为结束标志，中间为实际码值
     *
     * @return 条码值
     */
    public String getCodeData() {
        StringBuilder sb = new StringBuilder();
        int code_value;
        boolean allow_analyze_code = false;
        for (int i = 2; i < ((maxBarCode + 1) * 2); i++) {
            code_value = getBarCodeByBit(i / 2, ((i % 2) == 0) ? highBit : lowBit);
            if (!allow_analyze_code) {
                if (code_value == 02) {
                    allow_analyze_code = true;
                }
            } else {
                if (code_value == 3) {
                    break;
                }
                sb.append((char) code_value);
            }
        }
        return sb.toString();
    }

    public void setCodeData(String codeData) {
        this.codeData = codeData;
    }
}
